package com.ewanghuiju.app.ui.pintuan.activity;

import android.app.Activity;
import android.os.Handler;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.ewanghuiju.app.R;
import com.ewanghuiju.app.app.App;
import com.ewanghuiju.app.app.Constants;
import com.ewanghuiju.app.app.ForegroundCallbacks;
import com.ewanghuiju.app.base.RootActivity;
import com.ewanghuiju.app.base.contract.pintuan.PintuanHotContract;
import com.ewanghuiju.app.model.bean.response.PintuanShopListResponBean;
import com.ewanghuiju.app.presenter.pintuan.PintuanHotPresenter;
import com.ewanghuiju.app.ui.home.activity.SearchShopResultActivity;
import com.ewanghuiju.app.ui.pintuan.adapter.PintuanShopListAdapter;
import com.ewanghuiju.app.util.ClipboardUtils;
import com.ewanghuiju.app.util.LoadingDialogUtils;
import com.ewanghuiju.app.util.StartActivityUtil;
import com.ewanghuiju.app.util.nodoubleclick.AntiShake;
import com.ewanghuiju.app.widget.popupwindow.CommonSearchPopup;
import com.ewanghuiju.app.widget.popupwindow.PopupItemClickCallback;
import com.gyf.immersionbar.ImmersionBar;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author sj  qq:751943509
 * @class 热门团购
 * @time 2020/2/19
 */
public class PintuanHotActivity extends RootActivity<PintuanHotPresenter> implements PintuanHotContract.View, ForegroundCallbacks.Listener {
    @BindView(R.id.view_main)
    RecyclerView recyclerView;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.layout_screen_main)
    LinearLayout layoutScreenMain;

    @BindView(R.id.tv_screen_jg)
    TextView tvScreenJg;
    @BindView(R.id.tv_screen_xl)
    TextView tvScreenXl;
    @BindView(R.id.tv_screen_zh)
    TextView tvScreenZh;
    @BindView(R.id.iv_screen_jg)
    ImageView ivScreenJg;
    @BindView(R.id.iv_screen_xl)
    ImageView ivScreenXl;
    @BindView(R.id.tv_screen_rs)
    TextView tvScreenRs;
    @BindView(R.id.iv_screen_rs)
    ImageView ivScreenRs;

    private PintuanShopListAdapter pintuanShopListAdapter;

    private int currentPage = 1;

    private int spe_price = 0;

    private int sales = 0;

    private int people_number = 0;

    private int mType;
    private int classifyId;

    private CommonSearchPopup commonSearchPopup;


    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_pintuan_hot;
    }

    @Override
    protected void initEventAndData() {
        super.initEventAndData();
        // 注册监听
        ForegroundCallbacks.get(App.getInstance()).addListener(this);
        mType = getIntent().getIntExtra(Constants.PINTUAN_SHOP_TYPE, 1);
        classifyId = getIntent().getIntExtra(Constants.PINTUAN_SHOP, 0);
        if (mType > 1) {
            String strTitle = getIntent().getStringExtra(Constants.PINTUAN_SHOP_TITLE);
            tvTitle.setText(TextUtils.isEmpty(strTitle) ? "拼团商品" : strTitle);
        }
        intHotTuangouList();
    }

    /**
     * 热门团购 加载商品列表
     */
    private void intHotTuangouList() {
        layoutScreenMain.setBackgroundResource(R.drawable.corner_white_bottom_9);
        recyclerView.setNestedScrollingEnabled(false);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false);
        linearLayoutManager.setSmoothScrollbarEnabled(true);
        recyclerView.setLayoutManager(linearLayoutManager);
        pintuanShopListAdapter = new PintuanShopListAdapter(R.layout.item_pintuan_shop_hot);
        pintuanShopListAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                PintuanShopListResponBean pslBean = (PintuanShopListResponBean) adapter.getItem(position);
                new StartActivityUtil(mContext, PintuanShopDetailsActivity.class).putExtra(Constants.PINTUAN_SHOP_ID,
                        pslBean.getId())
                        .startActivity(true);
            }
        });
        recyclerView.setAdapter(pintuanShopListAdapter);
        refreshLayout.setEnableRefresh(true);//是否启用下拉刷新功能
        refreshLayout.setEnableLoadMore(true);
        refreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                LoadingDialogUtils.show(mContext);
                currentPage++;
                mPresenter.getData(mType, classifyId, currentPage, spe_price, sales, people_number);
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                LoadingDialogUtils.show(mContext);
                currentPage = 1;
                refreshLayout.resetNoMoreData();
                mPresenter.getData(mType, classifyId, currentPage, spe_price, sales, people_number);
            }
        });
        //首次进入显示
        LoadingDialogUtils.show(mContext);
        currentPage = 1;
        mPresenter.getData(mType, classifyId, currentPage, spe_price, sales, people_number);
    }

    @Override
    protected int getTitleIntText() {
        return R.string.hot_tuangou_title;
    }

    @Override
    protected void intSmartRefreshLayout() {
//        super.intSmartRefreshLayout();
    }


    @OnClick({R.id.layout_zh, R.id.layout_jg, R.id.layout_xl, R.id.layout_rs})
    public void doClick(View view) {
        if (AntiShake.check(view.getId())) {    //判断是否多次点击
            return;
        }
        currentPage = 1;
        LoadingDialogUtils.show(mContext);
        switch (view.getId()) {
            case R.id.layout_zh:
                reSetScreen();
                spe_price = 0;
                people_number = 0;
                sales = 0;
                tvScreenZh.setTextColor(ContextCompat.getColor(mContext, R.color.colorPrimary));
                mPresenter.getData(mType, classifyId, currentPage, spe_price, sales, people_number);
                break;
            case R.id.layout_jg:
                reSetScreen();
                people_number = 0;
                sales = 0;
                tvScreenJg.setTextColor(ContextCompat.getColor(mContext, R.color.colorPrimary));
                if (spe_price == 1) {
                    spe_price = 2;
                    ivScreenJg.setImageResource(R.mipmap.icon_screen_down);
                } else {
                    spe_price = 1;
                    ivScreenJg.setImageResource(R.mipmap.icon_screen_up);
                }
                mPresenter.getData(mType, classifyId, currentPage, spe_price, sales, people_number);
                break;
            case R.id.layout_rs:
                reSetScreen();
                spe_price = 0;
                sales = 0;
                tvScreenRs.setTextColor(ContextCompat.getColor(mContext, R.color.colorPrimary));
                if (people_number == 1) {
                    people_number = 2;
                    ivScreenRs.setImageResource(R.mipmap.icon_screen_down);
                } else {
                    people_number = 1;
                    ivScreenRs.setImageResource(R.mipmap.icon_screen_up);
                }
                mPresenter.getData(mType, classifyId, currentPage, spe_price, sales, people_number);
                break;
            case R.id.layout_xl:
                reSetScreen();
                spe_price = 0;
                people_number = 0;
                tvScreenXl.setTextColor(ContextCompat.getColor(mContext, R.color.colorPrimary));
                if (sales == 2) {
                    sales = 1;
                    ivScreenXl.setImageResource(R.mipmap.icon_screen_up);
                } else {
                    sales = 2;
                    ivScreenXl.setImageResource(R.mipmap.icon_screen_down);
                }
                mPresenter.getData(mType, classifyId, currentPage, spe_price, sales, people_number);
                break;
            default:
                break;
        }
    }

    /**
     * 重置顶部筛选
     */
    public void reSetScreen() {
        tvScreenZh.setTextColor(ContextCompat.getColor(mContext, R.color.color_666));
        tvScreenJg.setTextColor(ContextCompat.getColor(mContext, R.color.color_666));
        tvScreenRs.setTextColor(ContextCompat.getColor(mContext, R.color.color_666));
        tvScreenXl.setTextColor(ContextCompat.getColor(mContext, R.color.color_666));
        ivScreenJg.setImageResource(R.mipmap.icon_screen_normal);
        ivScreenRs.setImageResource(R.mipmap.icon_screen_normal);
        ivScreenXl.setImageResource(R.mipmap.icon_screen_normal);
    }

    @Override
    protected void initImmersionBar() {
        super.initImmersionBar();
        ImmersionBar.with(this).titleBar(mToolbar)
                .statusBarDarkFont(true)
                .init();
    }

    @Override
    public void showContent(List<PintuanShopListResponBean> list) {
        LoadingDialogUtils.dismissDialog_ios();
        if (list == null) {
            list = new ArrayList<>();
        }
        if (currentPage == 1) {
            if (list.size() < Constants.NUM_OF_PAGE) {
                refreshLayout.finishRefreshWithNoMoreData();
                refreshLayout.finishLoadMoreWithNoMoreData();
            } else {
                refreshLayout.finishRefresh();
            }
            pintuanShopListAdapter.setNewData(list);
        } else {
            if (list.size() < Constants.NUM_OF_PAGE) {
                refreshLayout.finishLoadMoreWithNoMoreData();
            } else {
                refreshLayout.finishLoadMore();
            }
            pintuanShopListAdapter.addData(list);
        }

        if (pintuanShopListAdapter.getData().size() > 0) {
            stateMain();
        } else {
            stateEmpty();
        }
//        refreshLayout.finishRefresh();
//        if (list != null) {
//            pintuanShopListAdapter.setNewData(list);
//            if (list.size() < Constants.NUM_OF_PAGE) {
//                refreshLayout.finishRefreshWithNoMoreData();
//            }
//        }
    }

    @Override
    public void showContentError() {
        LoadingDialogUtils.dismissDialog_ios();
        if (currentPage == 1) {
            refreshLayout.finishRefresh();
        } else {
            currentPage--;
            refreshLayout.finishLoadMore();
        }
        if (pintuanShopListAdapter.getData().size() == 0) {
            super.stateError();
        }
    }

    @Override
    public void showMoreContentError() {

        refreshLayout.finishLoadMore();
    }

    @Override
    public void stateError() {
        super.stateError();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // 移除监听
        ForegroundCallbacks.get(App.getInstance()).removeListener(this);
    }

    @Override
    public void onBecameForeground() {
        Activity mActivity = App.getInstance().getCurrentActivity();
        if (mActivity == null || !(mActivity instanceof PintuanHotActivity)) {
            return;
        }
        //判断粘贴板是否存在内容
        new Handler().postDelayed(new Runnable() {

            @Override
            public void run() {
                CharSequence mCharSequence = ClipboardUtils.getText();
                String clipboardText = TextUtils.isEmpty(mCharSequence) ? "" : mCharSequence.toString();
                if (clipboardText.length() > 0) {
                    ClipboardUtils.copyText("", "");
                    if (commonSearchPopup != null) {
                        commonSearchPopup.setContent(clipboardText);
                        commonSearchPopup.showPopupWindow();
                        return;
                    }
                    commonSearchPopup = new CommonSearchPopup(mContext,
                            new PopupItemClickCallback() {

                                @Override
                                public void onSureCallback(String text) {

                                    new StartActivityUtil(mContext, SearchShopResultActivity.class).putExtra(Constants.SEARCH_BODY,
                                            text)
                                            .startActivity(true);
                                }

                                @Override
                                public void onTextChangedCallback(String text) {

                                }
                            });
                    commonSearchPopup.setPopupGravity(Gravity.CENTER);
                    commonSearchPopup.setWidth(commonSearchPopup.getScreenWidth() * 2 / 3);
                    commonSearchPopup.setContent(clipboardText);
                    commonSearchPopup.showPopupWindow();
                }
            }
        }, 500);
    }

    @Override
    public void onBecameBackground() {

    }
}
